package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingEventTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo6Window {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    /**
     * 每隔5秒统计最近15秒单词的数量 -- 滑动窗口
     *
     */
    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    /**
     * window: 必须在分组之后使用，是对同一个key的数据做处理
     */
    //划分窗口，窗口大小为15秒，滑动时间为5秒
    //SlidingProcessingTimeWindows: 滑动的处理时间窗口
    val windowDS: WindowedStream[(String, Int), String, TimeWindow] = keyByDS
      .window(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)))

    //统计单词的数量
    val countDS: DataStream[(String, Int)] = windowDS.sum(1)

    countDS.print()

    env.execute()


  }

}
